home *** CD-ROM | disk | FTP | other *** search
- /*
- * TFNAME.C
- *
- * (c)Copyright 1992 by Tobias Ferber, All Rights Reserved.
- */
-
- #include <string.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <time.h>
-
- #ifndef MAXIMUM_PATHNAME_LENGTH
- #define MAXIMUM_PATHNAME_LENGTH 256L
- #endif
-
- /*** / AMIGA DEFINES / ***/
-
- #if defined(AMIGA)
- #define TEMP_PATH "T:" /* pathname for temporary files */
- #ifndef PSLASH
- #define PSLASH '/' /* concatenated to pathnames (if needed) */
- #endif /* !PSLASH */
-
- #ifndef PSEP
- #define PSEP ';' /* seperator for pathnames in environment vars */
- #endif /* !PSEP */
-
- /*** / MS-DOOF DEFINES / ***/
-
- #elif defined(__MSDOS__)
- #define TEMP_PATH getenv("TEMP")
-
- #ifndef PSLASH
- #define PSLASH '\\'
- #endif /* !PSLASH */
-
- #ifndef PSEP
- #define PSEP ';'
- #endif /* !PSEP */
-
- /*** / UNIX DEFINES / ***/
-
- #elif defined(unix)
- #define TEMP_PATH "/usr/tmp/"
- #ifndef PSLASH
- #define PSLASH '/'
- #endif /* !PSLASH */
-
- #ifndef PSEP
- #define PSEP ';'
- #endif /* !PSEP */
-
- #else /* unknown */
- #error "I don't know anything about pathnames on your machine!"
-
- #endif /* temp const */
-
- #define DEFAULT_TEMPFILE_FORMAT "temp%d.tmp"
-
- /*
- *
- * FUNCTION
- *
- * tfname -- build a temporary filename
- *
- * SYNOPSIS
- * #include "filecopy.h"
- *
- * tfn= tfname(fmt);
- * char *tfn;
- * char *fmt;
- *
- * DESCRIPTION
- *
- * This function tries to allocate MAXIMUM_PATHNAME_LENGTH chars for
- * a unique filename in the local area for temporary files.
- * Said filename will be a concatenation of the local TEMP_PATH,
- * a PSLASH (if needed) and given format string 'fmt' which should
- * contain one optional '%d' to hold a random number.
- * The allocated filename buffer will be returned; (char *)0L if sth.
- * went wrong.
- *
- * NOTE
- *
- * - The non-existance of the returned filename will not be verified.
- * - It's up to the caller to free() the returned filename buffer after
- * he/she's done with it.
- *
- */
-
- char *tfname(fmt)
- char *fmt;
- {
- char *tf;
-
- if( tf= (char *)malloc(MAXIMUM_PATHNAME_LENGTH * sizeof(char)) )
- {
- char *tpath;
- int n= 0;
-
- tpath= TEMP_PATH;
-
- #if defined(AMIGA)
- n= strlen(tpath);
- strcpy(tf, tpath);
-
- #elif defined(__MSDOS__)
- if(tpath)
- { char *t;
- for(t= tpath, n=0; *t && *t!=PSEP; tf[n++]= *t++) ;
-
- if(tf[n]!=PSLASH)
- tf[n++]= PSLASH;
- }
- else n= 0;
-
- #endif /* MACHINES */
-
- if( !(fmt && *fmt) )
- fmt= DEFAULT_TEMPFILE_FORMAT;
-
- { time_t t;
- time(&t);
- srand(t*270970*rand()+42);
- sprintf(&tf[n],fmt,rand());
- }
- }
-
- return tf;
- }
-